package com.yanqu.road.dao.impl.platform;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDao;
import com.yanqu.road.entity.platform.PlatformDeductIngotsOrder;

import java.sql.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class DeductIngotsDaoImpl extends TempDao {

	public List<PlatformDeductIngotsOrder> getPlatformDeductIngotsOrderList(long userId) {
		ResultSet rs = null;
		CallableStatement call = null;
		List<PlatformDeductIngotsOrder> dataList = new ArrayList<>();
		String sql = "select * from t_u_platform_deduct_ingots where userId =?";
		try {
			getDb().setConnName(DbNameStrategy.getUserDb());
			call = getDb().getConnection().prepareCall(sql);
			call.setLong(1, userId);
			rs = call.executeQuery();
			while (rs.next()) {
				dataList.add(initPlatformDeductIngotsOrder(rs));
			}
		} catch (Exception e) {
			throw new RuntimeException(e);
		} finally {
			getDb().closeConnection(call, rs);
		}
		return dataList;
	}

	public boolean addPlatformDeductIngotsOrder(PlatformDeductIngotsOrder data) {
		PreparedStatement call = null;
		String sql = "INSERT INTO t_u_platform_deduct_ingots(userId,orderNo,status,ingotCount,createTime,modifyTime,appid,platformUserId," +
				"gameId,childGameId,platformOrderNo,money,mallId,time,ext) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
		try {
			getDb().setConnName(DbNameStrategy.getUserDb());
			call = getDb().getConnection().prepareStatement(sql);
			int i = 1;
			call.setLong(i++, data.getUserId());
			call.setString(i++, data.getOrderNo());
			call.setInt(i++, data.getStatus());
			call.setLong(i++, data.getIngotCount());
			call.setTimestamp(i++, new Timestamp(System.currentTimeMillis()));
			call.setTimestamp(i++, new Timestamp(System.currentTimeMillis()));
			call.setString(i++, data.getAppid());
			call.setString(i++, data.getPlatformUserId());
			call.setString(i++, data.getGameId());
			call.setString(i++, data.getChildGameId());
			call.setString(i++, data.getPlatformOrderNo());
			call.setString(i++, data.getMoney());
			call.setString(i++, data.getMallId());
			call.setLong(i++, data.getTime());
			call.setString(i++, data.getExt());
            return call.executeUpdate() > 0;
		} catch (SQLException e) {
			logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
		} finally {
			getDb().closeConnection(call, null);
		}
		return false;
	}

	public PlatformDeductIngotsOrder getPlatformDeductIngotsOrder(String orderNo) {
		ResultSet rs = null;
		CallableStatement call = null;
		String sql = "select * from t_u_platform_deduct_ingots where orderNo=?";
		try {
			getDb().setConnName(DbNameStrategy.getUserDb());
			call = getDb().getConnection().prepareCall(sql);
			call.setString(1, orderNo);
			rs = call.executeQuery();
			while (rs.next()) {
				PlatformDeductIngotsOrder data = initPlatformDeductIngotsOrder(rs);
				return data;
			}
		} catch (SQLException e) {
			logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
		} finally {
			getDb().closeConnection(call, rs);
		}
		return null;
	}

	public boolean updatePlatformDeductIngotsOrderStatus(PlatformDeductIngotsOrder data) {
		String sql = "UPDATE t_u_platform_deduct_ingots SET status=?,modifyTime=?,executeType=? WHERE orderNo=?";
		getDb().setConnName(DbNameStrategy.getUserDb());
		Map<Integer, DbParameter> para = new HashMap<>();
		int i = 1;
		para.put(i++, new DbParameter(Types.INTEGER, data.getStatus()));
		para.put(i++, new DbParameter(Types.DATE, new Timestamp(System.currentTimeMillis())));
		para.put(i++, new DbParameter(Types.INTEGER, data.getExecuteType()));
		para.put(i++, new DbParameter(Types.VARCHAR, data.getOrderNo()));

		return getDb().executeNoneQuery(sql, para) > -1;
	}

	private PlatformDeductIngotsOrder initPlatformDeductIngotsOrder(ResultSet rs) throws SQLException {
		PlatformDeductIngotsOrder data = new PlatformDeductIngotsOrder();
		data.setUserId(rs.getLong("userId"));
		data.setOrderNo(rs.getString("orderNo"));
		data.setStatus(rs.getInt("status"));
		data.setIngotCount(rs.getLong("ingotCount"));
		data.setCreateTime(new Date(rs.getTimestamp("createTime").getTime()));
		data.setModifyTime(new Date(rs.getTimestamp("modifyTime").getTime()));
		data.setAppid(rs.getString("appid"));
		data.setPlatformUserId(rs.getString("platformUserId"));
		data.setGameId(rs.getString("gameId"));
		data.setChildGameId(rs.getString("childGameId"));
		data.setPlatformOrderNo(rs.getString("platformOrderNo"));
		data.setMoney(rs.getString("money"));
		data.setMallId(rs.getString("mallId"));
		data.setTime(rs.getLong("time"));
		data.setExt(rs.getString("ext"));
		data.setExecuteType(rs.getInt("executeType"));
		return data;
	}
}
